#include <iostream>
#include <exception>
#include "../Lib/toml11/toml.hpp"

#define COMMAND_FILE_FST "../../Config/command.toml"
#define COMMAND_FILE_SND "./command.toml"


int main(int argc, char *argv[])
{
    std::string command_file_path;
    if (FILE *file = fopen(COMMAND_FILE_FST, "r"))
    {
        fclose(file);
        command_file_path = COMMAND_FILE_FST;
    }
    else
    {
        command_file_path = COMMAND_FILE_SND;
    }

    auto command_file = toml::parse(command_file_path);

    std::vector<std::string> command_name_lists = toml::find<std::vector<std::string>>(command_file, "command_name_lists");

    std::vector<struct command_info> cmd_infos;
    for (const auto &command_name_list : command_name_lists)
    {
        std::cout << command_name_list << std::endl;
        const auto& cmd_config = toml::find(command_file, command_name_list);

        struct command_info cmd_info;

        cmd_info.cmd_type = toml::find<bool>(cmd_config, "cmd_type");
        cmd_info.name = toml::find<std::string>(cmd_config, "name");
        if (cmd_info.cmd_type)
        {
            cmd_info.shortcut = toml::find<std::string>(cmd_config, "shortcut");
            cmd_info.brief = toml::find<std::string>(cmd_config, "brief");
        }

        const auto &cmd_tables = toml::find<toml::array>(command_file, command_name_list, "command");
        for (const auto &cmd_table : cmd_tables)
        {
            struct command cmd;
            cmd.send = toml::find<std::string>(cmd_table, "send");
            cmd.receive = toml::find<std::string>(cmd_table, "receive");
            cmd_info.cmds.push_back(cmd);
        }

        cmd_infos.push_back(cmd_info);
    }

    for (const auto &cmd_info : cmd_infos)
    {
        std::cout << cmd_info.cmd_type << std::endl;
        std::cout << cmd_info.name << std::endl;
        std::cout << cmd_info.shortcut << std::endl;
        std::cout << cmd_info.brief << std::endl;

        for (const auto &cmd : cmd_info.cmds)
        {
            std::cout << cmd.send << std::endl;
            std::cout << cmd.receive << std::endl;
        }
    }
}
